package com.stu.vertx.hello.verticle;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;

public class RouterSequence04 extends AbstractVerticle {

    @Override
    public void start() throws Exception {

        // 创建HttpServer
        HttpServer server = vertx.createHttpServer();

        // 创建路由对象
        Router router = Router.router(vertx);


        // 匹配原则：默认就是当匹配成功之后就不再继续匹配
        /*router.route("/index/*").handler(request -> {
            request.response().end("Index");
        });
        router.route("/index/main").handler(request -> {
            request.response().end("IndexMain");
        });*/

        // order越小优先级越大
        /*router.route("/index/*").order(2).handler(request -> {
            request.response().end("Index");
        });
        router.route("/index/main").order(-1).handler(request -> {
            request.response().end("IndexMain");
        });*/

        //next()
        router.route("/index/*").order(2).handler(request -> {
        //	request.response().end("Index");
            System.out.println(1);
            request.next(); // 调下一个匹配规则
        });
        router.route("/index/main").order(-1).handler(request -> {
        //	request.response().end("IndexMain");
            System.out.println("2");
            request.next(); // 调下一个匹配规则
        });



        // 把请求交给路由处理--------------------(1)
        server.requestHandler(router::accept);
        server.listen(8888);
    }

    public static void main(String[] args) {
        Vertx.vertx().deployVerticle(new RouterSequence04());
    }
}
